#pragma once

#include "Header.h"
#include <math.h>

class FilterEQFast
{
public:
	double SampleRate = 44100;
	double Frequency = 2000;
	double Q = 1;
	double L = 1;  // - lowpass output sample
	double B = 1;  // - bandpass output sample
	double H = 1;  // - highpass output sample
	double N = 1;  // - notch output sample
	double F1 = 1;  // - Frequency control parameter
	double Q1 = 1;  // - Q control parameter
	double D1 = 1;  // - delay associated with bandpass output
	double D2 = 1;  // - delay associated with low-pass output

	double low_volyme;
	double mid_volyme;
	double hi_volyme;

	FilterEQFast()
	{
		Init();

		low_volyme = 1.0;
		mid_volyme = 1.0;
		hi_volyme = 1.0;
	}

	void Init()
	{
		Q1 = 1 / Q;
		F1 = 2 * sin(PI * Frequency / SampleRate);
	}

	void Reset()
	{
		Q = 1;
		L = 1;
		B = 1;
		H = 1;
		N = 1;
		F1 = 1;
		Q1 = 1;
		D1 = 1;
		D2 = 1;
		Init();
	}

	void Process(double& input)
	{
		L = D2 + F1 * D1;
		H = input - L - Q1 * D1;
		B = F1 * H + D1;

		// store delays
		D1 = B;
		D2 = L;
	}

	void ProcessTime(double* inputSamples, double* outputSamples, int length)
	{
		// Copy memver variables to stack will increase performance with 20%!!
		double _low_volyme = low_volyme;
		double _mid_volyme = mid_volyme;
		double _hi_volyme = hi_volyme;
		double _Q = Q;
		double _L = L;
		double _B = B;
		double _H = H;
		double _N = N;
		double _F1 = F1;
		double _Q1 = Q1;
		double _D1 = D1;
		double _D2 = D2;
		for (int i = 0; i < length; i++)
		{
			_L = _D2 + _F1 * _D1;

			_H = inputSamples[i] - _L - _Q1 * _D1;

			if (_H < 0.00001 && _H > -0.00001)
			{
				_H = 0;
			}

			_B = _F1 * _H + _D1;

			// store delays
			_D1 = _B;
			_D2 = _L;

			// outputs // L + H + B + N
			outputSamples[i] = _L * _low_volyme + _B * _mid_volyme + _H * _hi_volyme;
		}

		Q = _Q;
		L = _L;
		B = _B;
		H = _H;
		N = _N;
		F1 = _F1;
		Q1 = _Q1;
		D1 = _D1;
		D2 = _D2;
	}

	void SetBitrate(double bitrate)
	{
		SampleRate = bitrate;
	}
};